<?php
/**
 * Created by PhpStorm.
 * User: Luomo
 * Date: 2018/3/8
 * Time: 16:00
 */

namespace Biz\service\trade;

use Biz\model\trade\CustomizePayWay as mCustomizePayWay;
use Biz\pockets\constInterface\PayConfig;
use Doraemon\pockets\publicTrait\CheckConstants;
/**
 * @deprecated
 */
class CustomizePayWay implements PayConfig
{
    use CheckConstants;

    public static function admGetRecordList($params)
    {
        $whereAnd = [];
        if (isset($params['order_no']) && strlen($params['order_no']) > 0) {
            $whereAnd['order_no'] = $params['order_no'];
        }
        if (isset($params['pay_type']) && strlen($params['pay_type']) > 0) {
            $whereAnd['pay_type'] = $params['pay_type'];
        }
        if (isset($params['pay_no']) && strlen($params['pay_no']) > 0) {
            $whereAnd['pay_no'] = $params['pay_no'];
        }
        if (isset($params['type']) && strlen($params['type']) > 0) {
            $whereAnd['type'] = $params['type'];
        }
        if (isset($params['admin_id']) && strlen($params['admin_id']) > 0) {
            $whereAnd['admin_id'] = $params['admin_id'];
        }
        if (isset($params['add_time'])) {
            if (isset($params['add_time']['start']) && strlen($params['add_time']['start']) > 0) {
                $whereAnd['add_time[>=]'] = $params['add_time']['start'];
            }
            if (isset($params['add_time']['end']) && strlen($params['add_time']['end']) > 0) {
                $whereAnd['add_time[<=]'] = $params['add_time']['end'];
            }
        }
        $where['AND'] = $whereAnd;
        if (isset($params['LIMIT'])) {
            $where['LIMIT'] = $params['LIMIT'];
        }
        $list = mCustomizePayWay::getRecordList($where);
        $total = $list['total'];
        $list = $list['list'];

        return compact('list', 'total');
    }

    public static function addPayRecord($orderNos, $payType, $payNo, $payAmount, $adminId, $remark)
    {
        if (!array_key_exists($payType, mCustomizePayWay::PAY_TYPE_DESC)) {
            throw new \Exception('请选择正确的支付方式');
        }
        if ($orderNos) {
            $orderInfos = [];
            $orderTotalAmount = 0;
            $orderUid = NULL;
            foreach ($orderNos as $orderNo) {
                $orderInfo = Order::getCashierAmount($orderNo);
                $orderUid = $orderUid ?: $orderInfo['uid'];
                if ($orderUid != $orderInfo['uid']) {
                    throw new \Exception('订单所有者必须一致');
                }
                $orderTotalAmount = bcadd($orderTotalAmount, $orderInfo['pay_amount'], 2);
                $orderInfos[$orderNo] = $orderInfo;
            }
            if ($orderTotalAmount != $payAmount) {
                throw new \Exception('订单总金额和支付总金额必须一致');
            }
            foreach ($orderInfos as $orderInfo) {
                mCustomizePayWay::addPayRecord($orderInfo['order_no'], $orderInfo['pay_amount'], $payType, $payNo, $payAmount, $adminId, $remark);
                Pay::getPayOrderInfo($orderInfo, Pay::_PAY_WAYS_CUSTOMIZE_PAY_WAY, Pay::_PAY_ORDER_TYPE_BIZ);
                Pay::checkOrderPayStatus($orderInfo['order_no'], Pay::_PAY_ORDER_TYPE_BIZ);
            }
        } else {
            throw new \Exception('请选择订单');
        }

    }

    public static function addRefundRecord($refundNos, $payType, $payNo, $payAmount, $adminId, $remark)
    {
        if (!array_key_exists($payType, mCustomizePayWay::PAY_TYPE_DESC)) {
            throw new \Exception('请选择正确的支付方式');
        }
        if ($refundNos) {
            $refundInfos = [];
            $refundTotalAmount = 0;
            $refundUid = NULL;
            foreach ($refundNos as $refundNo) {
                $refundInfo = OrderRefund::admGetDetail($refundNo);
                $refundUid = $refundUid ?: $refundInfo['uid'];
                if ($refundUid != $refundInfo['uid']) {
                    throw new \Exception('订单所有者必须一致');
                }
                $refundInfo['total_amount'] = bcadd($refundInfo['refund_rmb'], $refundInfo['refund_shipping_amount'], 2);
                $refundTotalAmount = bcadd($refundTotalAmount, $refundInfo['total_amount'], 2);
                $refundInfos[$refundNo] = $refundInfo;
            }
            if ($refundTotalAmount != $payAmount) {
                throw new \Exception('退款订单总金额和退款总金额必须一致');
            }
            foreach ($refundInfos as $refundInfo) {
                mCustomizePayWay::addRefundRecord($refundInfo['refund_order_no'], $refundInfo['total_amount'], $payType, $payNo, $payAmount, $adminId, $remark);
            }
        } else {
            throw new \Exception('请选择退款订单');
        }
    }

    public static function getRecord($recordId)
    {
        return mCustomizePayWay::getRecordByRecordId($recordId);
    }

    public static function editRecord($recordId, $data)
    {
        mCustomizePayWay::updateRecord($recordId, $data);
    }
}